package com.qunar.toku.configuration;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.qunar.toku.TokuConst;
import com.qunar.toku.utility.XMLHelper;

public class ServiceConfig {
	private String servicename;
    private int serviceid;
	private List<ServerProfile> servers;
	private SocketPoolProfile socketPool;
	
	public SocketPoolProfile getSocketPool() {
		return socketPool;
	}

	public void setSocketPool(SocketPoolProfile socketPool) {
		this.socketPool = socketPool;
	}

	public List<ServerProfile> getServers() {
		return servers;
	}
	
	public String getServicename() {
		return servicename;
	}

	public int getServiceid() {
		return serviceid;
	}

	public static ServiceConfig getConfig(String serviceName) throws Exception {
		File file = new File(TokuConst.CONFIG_PATH);
		if (!file.exists()) {
			throw new Exception("toku.config not found:" + TokuConst.CONFIG_PATH);
		}
		
		Element xmlDoc = XMLHelper.GetXmlDoc(TokuConst.CONFIG_PATH);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        Node serviceNode = (Node) xpath.evaluate("//Service[@name='" + serviceName + "']", xmlDoc, XPathConstants.NODE);
        if(serviceNode == null){
        	printExceprion(0,serviceName);
        }
        
        ServiceConfig config = new ServiceConfig();
        config.servicename = serviceNode.getAttributes().getNamedItem("name").getNodeValue();
        Node idNode = serviceNode.getAttributes().getNamedItem("id");
        if(idNode == null){
        	printExceprion(4,serviceName);
        }
        config.serviceid = Integer.parseInt(idNode.getNodeValue());
        Node xnSocketPool = (Node) xpath.evaluate("Commmunication/SocketPool", serviceNode, XPathConstants.NODE);
        if(xnSocketPool == null){
        	printExceprion(1,serviceName);
        }
        config.socketPool = new SocketPoolProfile(xnSocketPool);
        
        NodeList xnServers = (NodeList) xpath.evaluate("Loadbalance/Server/add", serviceNode, XPathConstants.NODESET);
        if(xnServers == null || xnServers.getLength() == 0){
        	printExceprion(3,serviceName);
        }
        
        List<ServerProfile> servers = new ArrayList<ServerProfile>();
        for (int i = 0; i < xnServers.getLength(); i++) {
            servers.add(new ServerProfile(xnServers.item(i)));
        }
        config.servers = servers;
        config.servicename = serviceName;
        
        return config;
		
	}
	
	private static void printExceprion(int i, String serviceName) throws Exception{
    	switch (i) {
			case 0:
				throw new Exception("toku.config中没有发现" + serviceName + "服务节点!");
			case 1:
				throw new Exception("toku.config服务节点" + serviceName + "没有发现Commmunication/SocketPool配置!");
			case 3:
				throw new Exception("toku.config服务节点" + serviceName + "没有发现Loadbalance/Server/add配置!");
			case 4:
				throw new Exception("toku.config服务节点" + serviceName + "没有发现Service/id配置!");
			default:
				break;
		}
    }

}
